package com.wgt.io;

import java.io.*;

/**
 * Created by wgt on 2017/5/29.
 */
public class FileInputStearmDemo {

    public static void main(String[] args) throws IOException {
       // read2();
        readandwrite2();
    }

    public static void read() throws IOException {
        File file = new File("F:\\File\\aaa.txt");
        FileInputStream inputStream = new FileInputStream(file);
        String str = "";
        byte [] chars = new byte[1024];
        int n;
        while((n=inputStream.read(chars))!=-1){
            str +=new String(chars,0,n);
        }
        System.out.println(str);
        inputStream.close();
    }




    public static void read2() throws IOException {
        File file = new File("F:\\File\\bbb.txt");
        InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
        BufferedReader bf = new BufferedReader(reader);
        String str =null;
        while ((str=bf.readLine())!=null){
            if (str.startsWith("#")){
                System.out.println(str);
            }
        }
        bf.close();
    }


    /**
     * 读写
     * @throws IOException
     */
    public static void readandwrite() throws IOException {
        File file = new File("F:\\File\\aaa.txt");
        File outFile = new File("F:\\File\\b.txt");
        FileInputStream fileInputStream =new FileInputStream(file);
        FileOutputStream fileOutputStream = new FileOutputStream(outFile);

        String str = "";
        byte [] chars = new byte[1024];
        int n;

        while((n=fileInputStream.read(chars))!=-1){
            str += new String(chars,0,n);
            fileOutputStream.write(chars);
        }

        fileInputStream.close();
        fileOutputStream.close();
    }



    public static void readandwrite2() throws IOException {
        File file = new File("F:\\File\\bbb.txt");
        File outFile = new File("F:\\File\\c.txt");
        InputStreamReader reader = new InputStreamReader(new FileInputStream(file),"utf-8");
        BufferedReader bf = new BufferedReader(reader);
        FileWriter writer = new FileWriter(outFile,true);
        BufferedWriter bf_write = new BufferedWriter(writer);
        String str =null;
        while ((str=bf.readLine())!=null){
            if (str.startsWith("#")){
                System.out.println(str+"\r\n");
                bf_write.write(str+"\r\n");
            }
        }
        bf_write.close();
        writer.close();
        bf.close();
        reader.close();
    }

}
